package com.example.casper.jnuexperiment2025;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

public class BookDetailsActivity extends AppCompatActivity {

    private EditText editTextTitle;
    private EditText editTextPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_details);

        editTextTitle = findViewById(R.id.edit_text_book_title);
        editTextPrice = findViewById(R.id.edit_text_book_price);
        Button buttonSave = findViewById(R.id.button_save);

        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("book_title")) {
            String bookTitle = intent.getStringExtra("book_title");
            double bookPrice = intent.getDoubleExtra("book_price", 0.0);
            editTextTitle.setText(bookTitle);
            editTextPrice.setText(String.valueOf(bookPrice));
        }

        buttonSave.setOnClickListener(v -> {
            Intent resultIntent = new Intent();
            resultIntent.putExtra("book_title", editTextTitle.getText().toString());
            try {
                double price = Double.parseDouble(editTextPrice.getText().toString());
                resultIntent.putExtra("book_price", price);
            } catch (NumberFormatException e) {
                resultIntent.putExtra("book_price", 0.0);
            }
            resultIntent.putExtra("action", intent.getStringExtra("action"));
            resultIntent.putExtra("position", intent.getIntExtra("position", -1));

            setResult(RESULT_OK, resultIntent);
            finish();
        });
    }
}